//
//  OMHomeRecommendTableCell.m
//  OMENGO
//
//  Created by 梁尚嘉 on 2017/4/19.
//  Copyright © 2017年 Kamfat. All rights reserved.
//

#import "OMHomeRecommendTableCell.h"
#import "OMGoodTwoColumnFlowLayout.h"
#import "OMGoodTwoColumnCollectDelegater.h"
@interface OMHomeRecommendTableCell ()
@property (nonatomic, strong) UIImageView *iconBar;
@property (nonatomic, strong) UICollectionView *collectView;
@property (nonatomic, strong) NSArray *goods;
@property (nonatomic, strong) OMGoodTwoColumnCollectDelegater *delegater;
@end

@implementation OMHomeRecommendTableCell
#define kBarHeight (SM_SCREEN_WIDTH/375.f*20)
#define kBarMarginTop SM_SIZE(40)
- (void)configure
{
    [super configure];
    self.contentView.backgroundColor = SM_backGroundColor;

    _iconBar = [UIImageView new];
    _iconBar.image = [UIImage imageNamed:@"home_guess_bar"];
    [self.contentView addSubview:_iconBar];
    _iconBar.contentMode = UIViewContentModeScaleAspectFill;
    [_iconBar mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(@0);
        make.top.equalTo(@(kBarMarginTop));
        make.height.equalTo(@(kBarHeight));
    }];


    _collectView = [[UICollectionView alloc]initWithFrame:CGRectZero
                                     collectionViewLayout:[OMGoodTwoColumnFlowLayout new]];
    _delegater = [[OMGoodTwoColumnCollectDelegater alloc]initWithCollectionView:_collectView];
    _collectView.backgroundColor = SM_backGroundColor;
    _collectView.scrollEnabled = NO;
    _collectView.bounces = NO;
    [self.contentView insertSubview:_collectView belowSubview:_iconBar];
    [_collectView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_iconBar.mas_bottom);
        make.right.left.equalTo(@0);
        make.height.equalTo(@0);
    }];

}


- (void)configureCellWithObj:(id)obj
{
    if (self.goods == obj) {
        return;
    }
    self.goods = obj;
}

- (void)setGoods:(NSArray *)goods
{
    _goods = goods;
    self.delegater.goods = goods;
    [_collectView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.height.equalTo(@((GTCItemHeight+GTCItemSpacing)*[self.class lineCountWithNum:_goods.count]+GTCItemSpacing*2));

    }];
}

+ (CGFloat)cellHeightWithItems:(NSArray *)items
{

    return kBarHeight+kBarMarginTop+(GTCItemHeight+GTCItemSpacing)*[self lineCountWithNum:items.count]+GTCItemSpacing*2;
}


+ (NSUInteger)lineCountWithNum:(NSUInteger)num
{
    return num/2+(num%2==0?0:1);
}

@end
